package org.vectrics.user.client;


import org.vectrics.DataException;
import org.vectrics.VectricsException;
import java.util.Collection;
import java.util.Date;

import org.vectrics.client.webclient.*;
import org.apache.log4j.*;
import org.vectrics.user.User;
import org.vectrics.user.PersonSynch;


public class UserDataManager {
	private transient static Logger log = Logger.getLogger(UserDataManager.class);
//	private CommandManager commandManager = null;
	private static UserDataManager instance = null;

	/**
	 * Constructor
	 */
	public UserDataManager() {
		super();
		instance = this;
	}
	
	public static UserDataManager getInstance() {
		if (instance == null) {
			log.error("UserDataManager should be created before trying to use instance");
		}
		return(instance);
	}
	
	public void updateUser(PersonSynch userSynch) throws DataException {
		if (userSynch == null) {
			log.error("getUserDetails() - userHolder not given");
			throw new DataException("updateUser() - expected userSynch to be non-NULL");
		}
		if (userSynch.getPerson() == null)
			throw new DataException("updateUser() - expected userSynch to have user property assigned");
		UpdateUserCommand command = new UpdateUserCommand(userSynch.getPerson());
		CommandDispatcher.getInstance().execute(command);
			
	}
	
	/**
	 * Retrieve details about the user held by the UserSynch object.
	 */
	public void getUserDetails(PersonSynch userSynch) {
		User user = null;
		FindUserDetailsCommand command = new FindUserDetailsCommand(userSynch.getId());
		if (userSynch == null)
			log.error("getUserDetails() - userHolder not given");
		CommandDispatcher.getInstance().execute(command);
		userSynch.setPerson(command.getPerson());
		userSynch.setLastSynch(new Date());
	}
	
	
	public Collection getUserHoldersWithNameMatch(String firstName, String lastName) {
		FindUsersCommand command = new FindUsersCommand();
		command.setFirstName(firstName);
		command.setLastName(lastName);
		CommandDispatcher.getInstance().execute(command);
			
		Collection userIdList = command.getUserHolderList();
		return(userIdList);
	}

}
